<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

<script setup lang="ts">
// 导入 i18n 相关功能
const i18n = useI18n();
// 获取运行时配置
const runtimeConfig = useRuntimeConfig();

// 获取应用配置
const appConfig = useAppConfig();

// 根据应用配置设置主题
const theme = appConfig.systemConfig.siteInfo.siteTemplate;
// 获取系统配置
const systemConfig = appConfig.systemConfig || {};
const siteInfo = systemConfig.siteInfo || {};
const companyInfo = systemConfig.companyInfo || {};
// 获取 SEO 配置
const seoConfig = appConfig.seo || {};
// 使用系统配置或 SEO 配置或回退到 runtimeConfig 默认值
const siteName = siteInfo.siteTitle || runtimeConfig.public.siteName || "My Awesome Website";
const siteUrl = runtimeConfig.public.siteUrl || "https://example.com";
const siteDescription = siteInfo.siteDescription || `${siteName}`;
const author = companyInfo.companyName || "Awesome Web Team";
const keywords = siteInfo.siteKeywords || "awesome web, web development, nuxt3, vue, javascript, typescript";

// 社交媒体信息
// const social = seoConfig.social || {};
// const twitterHandle = social.twitter || "myawesomewebsite";
// const socialLinks = [
//   social.twitter ? `https://twitter.com/${social.twitter}` : "https://twitter.com/myawesomewebsite",
//   social.facebook || "https://www.facebook.com/myawesomewebsite",
//   social.linkedin || "https://www.linkedin.com/company/myawesomewebsite",
//   social.github ? `https://github.com/${social.github}` : "https://github.com/myawesomewebsite"
// ];

// 联系信息
const contactEmail = companyInfo.email || companyInfo.fax || "contact@example.com";
const contactPhone = companyInfo.mobilePhone || companyInfo.telephone || "+1-123-456-7890";
const contactAddress = companyInfo.companyAddress || "";

// 多语言配置
const i18nConfig = { defaultLocale: siteInfo.defaultLanguage || "zh", locales: ["zh", "en"] };

useHead({
  // 更完善的标题模板，包含网站名称
  titleTemplate: (title: any) => {
    return title ? `${title} - ${siteName}` : `${siteName}`;
  },
  meta: [
    { charset: "utf-8" },
    { name: "robots", content: "index,follow" },
    { name: "applicable-device", content: "pc,mobile" },
    { name: "format-detection", content: "telephone=no" },
    { name: "author", content: author },
    { name: "keywords", content: keywords },
    { name: "referrer", content: "no-referrer-when-downgrade" },
    {
      name: "viewport",
      content: "width=device-width, initial-scale=1, user-scalable=no, viewport-fit=cover"
    },
    {
      name: "description",
      content: siteDescription
    },
    // Open Graph 标签，优化社交媒体分享
    { property: "og:type", content: "website" },
    { property: "og:title", content: `${siteName}` },
    { property: "og:description", content: siteDescription },
    { property: "og:site_name", content: siteName },
    { property: "og:url", content: siteUrl },
    { property: "og:image", content: `${siteUrl}/og-image.jpg` },
    { property: "og:locale", content: () => (i18n.locale.value === "zh" ? "zh_CN" : "en_US") },
    { property: "og:locale:alternate", content: () => (i18n.locale.value === "zh" ? "en_US" : "zh_CN") },
    // Twitter 卡片标签
    { name: "twitter:card", content: "summary_large_image" },
    { name: "twitter:title", content: `${siteName}` },
    { name: "twitter:description", content: siteDescription },
    { name: "twitter:image", content: `${siteUrl}/og-image.jpg` },
    // 移动端优化
    { name: "theme-color", content: "#ffffff" },
    { name: "apple-mobile-web-app-capable", content: "yes" },
    { name: "apple-mobile-web-app-status-bar-style", content: "default" },
    // 安全相关
    { "http-equiv": "Content-Security-Policy", content: "upgrade-insecure-requests" },
    { "http-equiv": "X-UA-Compatible", content: "IE=edge" }
  ],
  link: [
    {
      rel: "icon",
      type: "image/x-icon",
      href: siteInfo.icon
    },
    {
      rel: "canonical",
      href: () => {
        return i18n.locale.value === "en" ? `${siteUrl}/en` : siteUrl;
      }
    },
    {
      rel: "alternate",
      hreflang: "zh-CN",
      href: siteUrl
    },
    {
      rel: "alternate",
      hreflang: "en-US",
      href: `${siteUrl}/en`
    },
    {
      rel: "alternate",
      hreflang: "x-default",
      href: siteUrl
    },
    // 预连接常用域名，提高性能
    {
      rel: "preconnect",
      href: "https://fonts.googleapis.com"
    },
    {
      rel: "preconnect",
      href: "https://fonts.gstatic.com",
      crossorigin: ""
    }
  ],
  // 添加结构化数据 (JSON-LD) 以优化 SEO
  script: [
    {
      type: "application/ld+json",
      children: () =>
        JSON.stringify({
          "@context": "https://schema.org",
          "@type": "WebSite",
          name: siteName,
          url: siteUrl,
          description: siteDescription,
          potentialAction: {
            "@type": "SearchAction",
            target: `${siteUrl}/search?q={search_term_string}`,
            "query-input": "required name=search_term_string"
          }
        })
    },
    {
      type: "application/ld+json",
      children: () =>
        JSON.stringify({
          "@context": "https://schema.org",
          "@type": "Organization",
          name: companyInfo.companyName || siteName,
          url: siteUrl,
          logo: siteInfo.siteLogoUrl || `${siteUrl}`,
          sameAs: "socialLinks",
          contactPoint: {
            "@type": "ContactPoint",
            telephone: contactPhone,
            contactType: "customer service",
            email: contactEmail,
            availableLanguage: ["Chinese", "English"]
          }
        })
    },
    {
      type: "text/javascript",
      innerHTML: siteInfo.extendCode
    }
  ]
});
</script>
